package com.my.ads.lib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.my.ads.lib.Config;
import com.my.ads.lib.activity.WifiActivity;
import com.my.ads.lib.utils.Utils;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by xuanyang on 2018/3/9.
 */

public class WifiReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {

        SharedPreferences sp = null;
        SharedPreferences.Editor editor = null;


        Log.i(Config.TagLog, "wifi");

        try {
            sp = context.getSharedPreferences(Config.WifiTarget, MODE_PRIVATE);
            editor = sp.edit();
            // 时间具体怎么弄再讨论
            if (Math.abs(System.currentTimeMillis() - sp.getLong(Config.AppStartTarget, 0L)) > 15 * 1000) {
                if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
                    if (Math.abs(System.currentTimeMillis() - sp.getLong(Config.TimeStartTarget, 0L)) > 20 * 60 * 1000) {
                        Utils.startActivity(context, WifiActivity.class);
                        editor.putLong(Config.TimeStartTarget, System.currentTimeMillis());
                        editor.apply();
                    }
                } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                    int netWorkState = Utils.getNetWorkState(context);
                    if (1 == netWorkState || 0 == netWorkState) {
                        Utils.startActivity(context, WifiActivity.class);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



}
